package com.xxj.fflink.menu;

import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.media.MediaPlayer;
import android.view.MotionEvent;

public class Util {

	public static Bitmap getBitMap(Context context,int imgID) {
		return BitmapFactory.decodeResource(context.getResources(), imgID);
	}
	
	public static int checkItemSelect(List<MenuItemAction> actions,MotionEvent event){
		if(null == actions || null == event){
			return -1;
		}
		
		int x = (int) event.getX();
		int y = (int) event.getY();
		System.out.println(" You click on:(" + x + "," + y + ").");
		
		Rect rect;
		for (MenuItemAction action : actions) {
			rect = action.getDimension();
			if(rect.contains(x, y)){
				return action.getGameAction();
			}
		}
		return -1;
	}
	
	public static MediaPlayer initMediaPlayer(Activity activity, int mediaId, boolean isLooping){
		MediaPlayer mp = MediaPlayer.create(activity, mediaId);
		try {
			mp.prepare();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		mp.setLooping(isLooping);
		
		return mp;
	}
	
	public static MediaPlayer initMediaPlayer(Activity activity, int mediaId){
		return initMediaPlayer(activity,mediaId,false);
	}
}
